package com.youkeda.meetingroom.service.impl;

import com.youkeda.meetingroom.dao.dataobject.MrBookRecord;
import com.youkeda.meetingroom.dao.dataobject.MrMeetingRoom;
import com.youkeda.meetingroom.dao.dataobject.MrMeetingRoomPassword;
import com.youkeda.meetingroom.dao.dataobject.MrUser;
import com.youkeda.meetingroom.dao.mapper.MrBookRecordMapper;
import com.youkeda.meetingroom.dao.mapper.MrMeetingRoomMapper;
import com.youkeda.meetingroom.dao.mapper.MrMeetingRoomPasswordMapper;
import com.youkeda.meetingroom.dao.mapper.MrUserMapper;
import com.youkeda.meetingroom.model.MeetingRoom;
import com.youkeda.meetingroom.model.MeetingRoomPassword;
import com.youkeda.meetingroom.model.Result;
import com.youkeda.meetingroom.service.PasswordService;
import com.youkeda.meetingroom.util.IdUtils;
import com.youkeda.meetingroom.util.PwdUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Random;

/**
 * PasswordServiceImpl的注释
 *
 * @author: Awake
 * @date: 2019-07-03 17:35
 **/
@Service
public class PasswordServiceImpl implements PasswordService {

    @Resource
    MrUserMapper mrUserMapper;


    @Resource
    MrMeetingRoomPasswordMapper mrMeetingRoomPasswordMapper;
    @Resource
    MrMeetingRoomMapper mrMeetingRoomMapper;


    /**
     * 生成一条密码
     * 生成成功则返回明文密码
     *
     * @param userId        用户主键
     * @param meetingRoomId 会议室主键
     * @return String 明文密码
     */
    @Override
    public Result generagePwd(String userId, String meetingRoomId) {
        Result result = new Result();
        //判断传值为空
        if(Strings.isBlank(userId)||Strings.isBlank(meetingRoomId)){
            result.setSuccess(false);
            result.setErrorMessage("用户主键或会议室主键为空");
            return result;
        }
        //新建密码记录
        String salt = IdUtils.get32Id();
        String plainPwd = String.valueOf(new Random().nextInt(1000000));
        String encPwd = PwdUtils.md5Password(plainPwd,salt);
        MrMeetingRoomPassword roomPassword = new MrMeetingRoomPassword();
        roomPassword.setId(IdUtils.get32Id());
        roomPassword.setValid(true);
        roomPassword.setPwd(encPwd);
        roomPassword.setSalt(salt);
        roomPassword.setMeetingRoomId(meetingRoomId);
        roomPassword.setUserId(userId);

        Date duetime = new Date();
        duetime.setTime(System.currentTimeMillis() + 30 * 60 * 1000);
        roomPassword.setDueTime(duetime);

        //插入密码记录
        mrMeetingRoomPasswordMapper.insert(roomPassword);

        result.setSuccess(true);
        result.setData(plainPwd);
        return result;
    }

    /**
     * 用户手动获取密码
     *
     * @param bookRecordId 预约主键
     * @return MeetingRoomPassword
     */

    @Resource
    MrBookRecordMapper mrBookRecordMapper;

    @Override
    public Result getPwd(String bookRecordId) {
      return new Result();
    }

    /**
     * 校验明文密码是否正确，正确则将密码记录标记为无效
     *
     * @param pwd          明文密码
     * @param bookRecordId 预约主键
     * @return 正确信息
     */
    @Override
    public boolean checkPwd(String pwd, String bookRecordId) {

        MrBookRecord mrBookRecord = mrBookRecordMapper.selectByPrimaryKey(bookRecordId);
        String userId = mrBookRecord.getUserId();
        MrUser mrUser = mrUserMapper.selectByPrimaryKey(userId);
        //加密后的密码
        String enPwb = PwdUtils.md5Password(pwd,mrUser.getSalt());
        String dataBasePwb = mrMeetingRoomPasswordMapper.selectPwdByUserId(userId).getPwd();
        if (pwd.equals(dataBasePwb)) {
            return true;
        } else {
            return false;
        }
    }

}
